/*
 * GDO GeometryFactory
 * 
 * initial implementation : 31.8.2007
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;

namespace Petra.Gis.DataServices.GDO
{
  public unsafe partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates Path
     * from intergraph blob structure
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    private static GeometryBase binaryToPath(byte[] vData, int arrayOffset, System.Type parentGeomType)
    {

      /*Guid ingrGUID;
      fixed (byte* bGUID = &vData[arrayOffset])
      {
        ingrGUID = (*((Guid*)bGUID));
      }
      if (ingrGUID == getGuid[GeometryType.Boundary]) { }*/

      Path oGeom = new Path();

      int pos;
      fixed (byte* bPtr = &vData[arrayOffset])
      {
        int* lGeomSize;
        GeometryBase tmpGeom;

        //skip position 0, 16 bytes Collection GUID
        //get num of geoms in collection position 16
        pos = 16;
        int* numGeoms = (Int32*)(bPtr + pos);

        //step over all items
        //set pos to binary size of first geom
        pos += 4;
        for (int i = 0; i < *numGeoms; i++)
        {
          lGeomSize = (Int32*)(bPtr + pos);
          tmpGeom = createGeometry(vData, (arrayOffset + pos + 4), typeof(Path));
          oGeom.Add(tmpGeom);
          pos = pos + 4 + *lGeomSize;
        }
      }
      return oGeom;
    }






    /***********************************************
     * 28.8.2007
     * 
     * creates intergraph blob structure 
     * from Path
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static byte[] pathToBinary(GeometryBase geometry)
    {
      //cast to Polygon first
      Path _coll = (Path)geometry;

      //if only one element in colection skip collection header
      /*if (_coll.Count == 1)
        return saveGeometryBlob(_coll[0]);*/

      int lBlobSize;
      Guid gGeomGUID;

      lBlobSize = 0;

      System.IO.MemoryStream oIMStream = new System.IO.MemoryStream(lBlobSize);
      System.IO.BinaryWriter mBWriter = new System.IO.BinaryWriter(oIMStream);

      oIMStream.Position = 0;
      //write GUID
      gGeomGUID = getGuid[GeometryType.CompositePolyline];
      mBWriter.Write(gGeomGUID.ToByteArray());

      //call GeometryCollection-collectionItemsToBinary()
      mBWriter.Write(collectionItemsToBinary(_coll, 0));
      return oIMStream.ToArray();
    }
  }
}
